import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ytbusiness/config/style.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/page/auto_makers/shop_home/order_details/order_info.dart';
import 'package:ytbusiness/page/auto_makers/shop_home/order_widget/enquiry_item.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';
import 'package:ytbusiness/widgets/widgets.dart';

///待发货
class WaitingDelivery extends StatefulWidget {
  @override
  _WaitingDeliveryState createState() => _WaitingDeliveryState();
}

class _WaitingDeliveryState extends State<WaitingDelivery> {
  List<String> typeList = ["原厂原包", "品牌", "拆车", "OEM"];
  List<List<TextEditingController>> deliveryOrder = [];

  bool isShow = false;//默认隐藏
  int checkIndex = 0; //当前选中索引
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    addTracking();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        //输入框失焦
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: Scaffold(
        backgroundColor: YTStyle.bgColor,
        appBar: customAppBar(context, title: "待发货"),
        body: CustomScrollView(
          slivers: [
            //订单信息
            SliverToBoxAdapter(
              child: OrderInfo(),
            ),

            //联系方式
            SliverToBoxAdapter(
              child: Container(
                padding:
                EdgeInsets.only(bottom: setWidth(10), top: setWidth(10)),
                color: Colors.white,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    link(image: "blue_kefu.png",title: "在线联系",phone: "10086"),
                    link(image: "blue_phone_link.png",title: "电话联系",phone: "10086"),
                  ],
                ),
              ),
            ),

            //询价信息
            SliverToBoxAdapter(
              child: Container(
                margin: EdgeInsets.only(top: setWidth(15)),
                padding: EdgeInsets.only(
                    top: setWidth(18),
                    left: setWidth(15),
                    bottom: setWidth(13)),
                decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border(
                        bottom: BorderSide(
                            color: Color(0xffF0F0F0), width: setWidth(0.5)))),
                child: Text(
                  "询价信息",
                  style: TextStyle(
                      color: Color(0xff323232),
                      fontSize: setSp(15),
                      fontWeight: FontWeight.w600),
                ),
              ),
            ),

            //询价列表
            SliverList(
                delegate: SliverChildBuilderDelegate(
                      (context, index) {
                    return EnquiryItem(
                      checkIndex: checkIndex,
                      onCheckUpdate: (value){
                        print(value);
                      },
                    );
                  },
                  //默认显示一条
                  childCount: isShow?(typeList.length >0 ? typeList.length:0):1,
                )),
            //隐藏显示
            SliverToBoxAdapter(
              child: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: (){
                  setState(() {
                    isShow = !isShow;
                  });
                },
                child: Container(
                  alignment: Alignment.center,
                  margin: EdgeInsets.only(bottom: setWidth(12)),
                  padding: EdgeInsets.only(
                      top: setWidth(12),
                      bottom: setWidth(12)),
                  color: Colors.white,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        child: Text(
                          isShow?"隐藏未报价的${typeList.length-1}个商品":"已隐藏待发货的${typeList.length-1}个商品",
                          style: TextStyle(
                              color: Color(0xff989898),
                              fontSize: setSp(14)),
                        ),
                      ),

                      Container(
                        margin: EdgeInsets.only(left: setWidth(6),right: setWidth(6)),
                        child: Text(
                          isShow?"收起":"展开",
                          style: TextStyle(
                              color: Color(0xff989898),
                              fontSize: setSp(14),
                              fontWeight: FontWeight.w600),
                        ),
                      ),
                      SizedBox(width: setWidth(12),
                        height: setWidth(6),
                        child: Image.asset(isShow?"assets/images/shouqi.png":"assets/images/zhankai.png",width: setWidth(12),
                          height: setWidth(6),fit: BoxFit.cover,),
                      ),

                    ],
                  ),
                ),
              ),
            ),
            //结算信息
            SliverToBoxAdapter(
              child: settlementInfo(price:"1259.00",serve: "3280.00",sum: "1259.00"),
            ),
            SliverToBoxAdapter(
              child: SizedBox(
                height: setWidth(12),
              ),
            ),

            //发货信息
            SliverToBoxAdapter(
              child: Container(
                padding: EdgeInsets.only(
                    top: setWidth(18),
                    left: setWidth(15),
                    right: setWidth(15),
                    bottom: setWidth(30)),
                color: Colors.white,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      child: Text(
                        "发货信息",
                        style: TextStyle(
                            color: Color(0xff323232),
                            fontSize: setSp(16),
                            fontWeight: FontWeight.w600),
                      ),
                    ),
                    SizedBox(
                      height: setWidth(13),
                    ),
                    Container(
                      child: Text(
                        "发货信息默认不发自提件，如果发送自提件，请于采购方一 致，以免造成投诉",
                        style: TextStyle(
                            color: Color(0xff989898), fontSize: setSp(13)),
                      ),
                    ),
                    SizedBox(
                      height: setWidth(28),
                    ),

                    //运单列表
                    Container(
                      child: ListView.builder(
                          physics: NeverScrollableScrollPhysics(),
                          itemCount: deliveryOrder.length,
                          shrinkWrap: true,
                          itemBuilder: (context, index) {
                            return tracking(
                                transportType: deliveryOrder[index][0],
                                transportNum: deliveryOrder[index][1]);
                          }),
                    ),
                    SizedBox(
                      height: setWidth(23),
                    ),
                    GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {

                      },
                      child: Container(
                        height: setWidth(44),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text("确认发货", style: TextStyle(color: Colors.white,
                                fontSize: setSp(18),
                                fontWeight: FontWeight.w600),),
                          ],
                        ),
                        decoration: BoxDecoration(
                            color: YTTheme.theme,
                            borderRadius: BorderRadius.circular(2)
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),


          ],
        ),
      ),
    );
  }

  
  //运单
  Widget tracking(
      {TextEditingController transportType, TextEditingController transportNum}) {
    return Container(
      margin: EdgeInsets.only(bottom: setWidth(34)),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          //快递方式
          Container(
            child: Row(
              mainAxisAlignment:
              MainAxisAlignment.start,
              crossAxisAlignment:
              CrossAxisAlignment.center,
              children: [
                Container(
                  child: Text("快递方式:"),
                ),
                SizedBox(
                  width: setWidth(9),
                ),
                Expanded(
                    child: Container(
                      child: TextField(
                        controller: transportType,
                        textAlign: TextAlign.start,
                        maxLines: 1,
                        keyboardType: TextInputType.multiline,
                        autofocus: false,
                        decoration: InputDecoration(
                          enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(2.0),
                            borderSide: BorderSide(color: Color(0xff989898),width: setWidth(0.5)),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(2.0),
                            borderSide: BorderSide(color: Color(0xff989898),width: setWidth(0.5)),
                          ),
                          contentPadding: EdgeInsets.only(
                              top: setWidth(3), left: setWidth(12)),
                          hintText: "请填写快递方式",
                          hintStyle: TextStyle(
                              color: Color(0xffC6C9CE), fontSize: setSp(16)),
                        ),
                      ),
                    )),
                SizedBox(
                  width: setWidth(67),
                ),
              ],
            ),
            height: setWidth(40),
          ),
          SizedBox(
            height: setWidth(15),
          ),
          //快递单号
          Container(
            child: Row(
              mainAxisAlignment:
              MainAxisAlignment.start,
              crossAxisAlignment:
              CrossAxisAlignment.center,
              children: [
                Container(
                  child: Text("快递单号:"),
                ),
                SizedBox(
                  width: setWidth(9),
                ),
                Expanded(
                    child: Container(
                      child: TextField(
                        controller: transportNum,
                        textAlign: TextAlign.start,
                        maxLines: 1,
                        keyboardType: TextInputType.multiline,
                        autofocus: false,
                        decoration: InputDecoration(
                          enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(2.0),
                            borderSide: BorderSide(color: Color(0xff989898),width: setWidth(0.5)),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(2.0),
                            borderSide: BorderSide(color: Color(0xff989898),width: setWidth(0.5)),
                          ),
                          contentPadding: EdgeInsets.only(
                              top: setWidth(3), left: setWidth(12)),
                          hintText: "请填写快递单号",
                          hintStyle: TextStyle(
                              color: Color(0xffC6C9CE), fontSize: setSp(16)),
                        ),
                      ),
                    )),
                SizedBox(
                  width: setWidth(7),
                ),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    addTracking();
                  },
                  child: Container(
                    child: Text("增加运单", style: TextStyle(
                        color: Colors.white, fontSize: setSp(12)),),
                    padding: EdgeInsets.only(top: setWidth(5),
                        bottom: setWidth(5),
                        left: setWidth(6),
                        right: setWidth(6)),
                    decoration: BoxDecoration(
                        color: YTTheme.theme,
                        borderRadius: BorderRadius.circular(2)
                    ),
                  ),
                )
              ],
            ),
            height: setWidth(40),
          ),
        ],
      ),
    );
  }

  //添加运单列表
  addTracking() {
    TextEditingController _transportType = TextEditingController();
    TextEditingController _transportNum = TextEditingController();
    setState(() {
      deliveryOrder.add([_transportType, _transportNum]);
    });
  }
}